package com.jerry.huihome.module.home.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseActivity;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.fragment.FindFragment;
import com.jerry.huihome.module.find_near_mine.fragment.Find_Fragment;
import com.jerry.huihome.module.home.fragment.DrawHomeFragment;
import com.jerry.huihome.module.home.fragment.LiveFragment;
import com.jerry.huihome.module.register.fragment.MineFragment;
import com.jerry.huihome.utils.EventMessage;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import greendao.LoreDetailTableDao;

public class MainActivity extends BaseActivity
{
    SlidingMenu menu = null;
    private FragmentManager manager;
    private LoreDetailTableDao dao;
    private FragmentTransaction fragmentTransaction;
    private EventBus eventBus;

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_main;
    }

    /**
     * 初始化视图
     */
    @Override
    protected void initViews(){
        menu = new SlidingMenu(this);

        // 设置左边侧边栏
        menu.setMode(SlidingMenu.LEFT);

        // 设置什么位置可以拉动侧边栏
        //menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

        // 中间阴影部分宽度
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.sliding_shadow);

        // 侧边栏后面的布局的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        //menu.setBehindOffset(DensityUtil.dip2px(120));

        menu.setFadeDegree(0.35f);

        // 把侧边栏附加到当前的Activity
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

        // 设置侧边栏的布局
        menu.setMenu(R.layout.main_left_menu);

        manager = getSupportFragmentManager();
        //
        initMainPage();
        eventBus = EventBus.getDefault();
        //
       /* Map<String, String> params = new HashMap<>();
        params.put("tag", "1");
        //
        MyFragment myFragment = BaseFragment.newInstance(MyFragment.class, params);
        // 默认先显示首页
        manager.beginTransaction().replace(R.id.fl_main, myFragment).commit();
        dao = getDaoSession().getLoreDetailTableDao();
        // 添加
        LoreDetailTable loreDetail = new LoreDetailTable();
        loreDetail.setTitle("标题内容");
        loreDetail.setImageUrl("111111111");
        dao.insert(loreDetail);

        // 查询
        Query<LoreDetailTable> query = dao.queryRawCreate("_id = ?",1);
        List<LoreDetailTable> list = query.list();


        // 使用QueryBuilder查询
        QueryBuilder<LoreDetailTable> qb = dao.queryBuilder();
        qb.where(LoreDetailTableDao.Properties.Id.eq(1));
        long count = qb.buildCount().count();

        // 使用链式法
        list = dao.queryBuilder()
                .where(LoreDetailTableDao.Properties.Title.eq("标题内容"))
                .orderAsc(LoreDetailTableDao.Properties.Id)
                .list();

        // 查找某一个对象
        LoreDetailTable loreDetailTable =
                dao.queryBuilder()
                        .where(LoreDetailTableDao.Properties.Id.eq(1))
                        .unique();

        // 删除
        //dao.deleteByKey(1l);
        dao.delete(loreDetailTable);

        // 修改
        loreDetailTable.setTitle("修改标题");
        dao.update(loreDetailTable);*/
    }
    private void initMainPage(){
        changePage("drawHouse",null);
    }

    public void changePage(String tag,Map<String, String> params){
        Fragment fragmentByTag = manager.findFragmentByTag(tag);
        fragmentTransaction = manager.beginTransaction();
        if(params == null){
            params = new HashMap<>();
            params.put("tag", tag);
        }
        if(fragmentByTag != null && fragmentByTag.isAdded()){
            if(lastFragment != null){
                fragmentTransaction.hide(lastFragment);
            }
            fragmentTransaction.show(fragmentByTag);
            lastFragment = fragmentByTag;
            fragmentByTag.onResume();
        }else{
            Fragment fragment = null;
            switch(tag){
                case "drawHouse":
                    fragment = BaseFragment.newInstance(DrawHomeFragment.class, params);
                    break;
                case "seek":
                    fragment = BaseFragment.newInstance(FindFragment.class, params);
                    break;
                case "live":
                    fragment = BaseFragment.newInstance(LiveFragment.class, params);
                    break;
                case "mine" :
                    fragment = BaseFragment.newInstance(MineFragment.class,null);
                    break;
                case "find":
                    fragment = BaseFragment.newInstance(Find_Fragment.class, params);
                    break;
            }
            fragmentTransaction.replace(R.id.fl_main, fragment, tag).commit();
            // 把要显示的Fragment保存起来
            lastFragment = fragment;
        }
    }

    private Fragment lastFragment = null;

    public void getHouseView(View view){
        changePage("drawHouse",null);
        menu.toggle();
    }

    public void getSeek(View view){
        changePage("seek",null);
        menu.toggle();
    }

    public void getConstruction(View view){
        changePage("live",null);
        menu.toggle();
    }

    public void getFind(View view){
        changePage("find",null);
        menu.toggle();
    }

    public void getMine(View view){
        changePage("mine",null);
        menu.toggle();
    }

    /**
     * 加载数据
     */
    @Override
    protected void loadData()
    {
        Map<String, Object> param = new HashMap<>();
        param.put("pageindex", 3);
        OkHttpUtils.postSubmitForm(Urls.HOTEL_LIST, param);
    }

    public void changePage(View view){
        menu.toggle();
    }

    /**
     * 获取具体的结果
     *
     * @param message
     */
    public void postEventResult(EventMessage message)
    {
        String type = message.type;

        if (Urls.HOTEL_LIST.equals(type))
        {
            Log.d("getResult", "onGetResult() returned: " + message.result);
        }

        if(type == "seek"){
                changePage(message.result,null);
        }else if(type == "find"){
            changePage(message.result,null);
        }else if(type == "activity"){
            Map<String,String> params = new HashMap<>();
            params.put("tag","activity");
            changePage("find",params);
        }else if(type == "place"){
            Map<String,String> params = new HashMap<>();
            params.put("tag","place");
            changePage("seek",params);
        }
    }


}
